﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DataAccess
{
   public  class Data
    {
       
       public static DataTable ExeStoredWithParamToDataTable(string storedName, params object[] prms)
       {
           ConnectDB cn = new ConnectDB();
           DataTable dt = new DataTable();
           SqlConnection conn =new  SqlConnection(cn.ConnectionString());
           SqlCommand cmd = new SqlCommand();
           try
           {
               cmd.Connection = conn;
               conn.Open();
               cmd.CommandText = storedName;
               cmd.CommandType = CommandType.StoredProcedure;
               for (int i = 0; i < prms.Length; i += 2)
               {
                   SqlParameter pa = new SqlParameter(prms[i].ToString(), prms[i + 1]);
                   cmd.Parameters.Add(pa);
               }
               SqlDataAdapter da = new SqlDataAdapter();
               da.SelectCommand = cmd;
               da.Fill(dt);
               conn.Close();
               cmd.Dispose();
               
           }
           catch { }
           finally
           {
               if (conn.State == ConnectionState.Open)
                   conn.Close();
               conn.Dispose();
           }
           return dt;
       }

       public static DataTable ExecuteStoredProcedureNoParam(string procName)
       {
           try
           {
               ConnectDB cn = new ConnectDB();
               SqlConnection con = new SqlConnection(cn.ConnectionString());
              
                   DataTable dt = new DataTable();
                   SqlCommand cmd = new SqlCommand(procName, con);
                   cmd.CommandType = CommandType.StoredProcedure;
                   SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                   adapter.Fill(dt);
                   return dt;
             
           }
           catch (Exception)
           {
              
               return null;
           }
       }
       public static DataTable ExeStoredWithParamToDataTabe_Duong(string storedName, String cardNo, int time)
       {
           ConnectDB cn = new ConnectDB();
           DataTable dt = new DataTable();
           SqlConnection conn = new SqlConnection(cn.ConnectionString());
           SqlCommand cmd = new SqlCommand();
           try
           {
               cmd.Connection = conn;
               conn.Open();
               cmd.CommandText = storedName;
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add("CardNo", cardNo);
               cmd.Parameters.Add("Time", time);
               SqlDataAdapter da = new SqlDataAdapter();

               da.SelectCommand = cmd;
               da.Fill(dt);
               conn.Close();
               cmd.Dispose();
           }
           catch { }
           finally
           {
               if (conn.State == ConnectionState.Open)
                   conn.Close();
               conn.Dispose();
           }
           return dt;
       }
    }
}
